package com.itheima.api.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.pms.PmsProductAttributeMapper;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.dto.pms.PmsProductCategoryAttributeRelationDto;
import com.itheima.vo.pojo.PmsProductAttribute;
import com.itheima.vo.pojo.PmsProductCategoryAttributeRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements PmsProductAttributeService {

    @Autowired
    private PmsProductCategoryAttributeRelationService pmsProductCategoryAttributeRelationService;

    @Autowired
    private PmsProductAttributeMapper pmsProductAttributeMapper;

    /**
     * 1.根据商品分类的id获取商品属性及属性分类
     * @param productCategoryId
     * @return
     */
    @Override
    public AjaxResult getAttrInfo(Integer productCategoryId) {

        //根据id获取PmsProductCategoryAttributeRelation集合
        List<PmsProductCategoryAttributeRelation> pcarList = pmsProductCategoryAttributeRelationService.getProductAttributeId(productCategoryId);
        //从该集合中获取或有的productAttributeId
        ArrayList<PmsProductCategoryAttributeRelationDto> pabIdList = new ArrayList<PmsProductCategoryAttributeRelationDto>();
        for (PmsProductCategoryAttributeRelation pacr : pcarList) {
            //得到paId,到pa表内查询pacId
            long paId = pacr.getProductAttributeId();
            /*LambdaQueryWrapper<PmsProductAttribute> lqw = new LambdaQueryWrapper<>();
            lqw.eq(PmsProductAttribute::getId,paId);*/
            PmsProductAttribute pmsProductAttribute = pmsProductAttributeMapper.selectById(paId);
            //得到pacId
            long pacId = pmsProductAttribute.getProductAttributeCategoryId();
            //创建返回的对象,封装数据
            PmsProductCategoryAttributeRelationDto pcarDto = new PmsProductCategoryAttributeRelationDto();
            pcarDto.setAttributeId(paId);
            pcarDto.setAttributeCategoryId(pacId);
            pabIdList.add(pcarDto);
        }
        return AjaxResult.success(pabIdList);
    }

    /**
     * 2.添加商品属性信息
     * @param pmsProductAttribute
     * @return
     */
    @Override
    public AjaxResult create(PmsProductAttribute pmsProductAttribute) {
        pmsProductAttributeMapper.insert(pmsProductAttribute);
        return AjaxResult.success("添加成功");
    }

    /**
     * 3.批量删除商品属性
     * @param ids
     * @return
     */
    @Override
    public AjaxResult deleteBatch(ArrayList<Integer> ids) {
        for (Integer id : ids) {
            pmsProductAttributeMapper.deleteById(id);
        }
        return AjaxResult.success("删除成功");
    }

    /**
     * 4.根据分类查询属性列表或参数列表
     * @param cid
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public AjaxResult getList(Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        //构造查询条件
        LambdaQueryWrapper<PmsProductAttribute> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductAttribute::getProductAttributeCategoryId , cid);
        lqw.eq(PmsProductAttribute::getType,type);
        lqw.orderByAsc(PmsProductAttribute::getSort);
        //构造分页对象
        Page<PmsProductAttribute> pmsProductAttributePage = new Page<>(pageNum,pageSize);
        Page<PmsProductAttribute> page1 = pmsProductAttributeMapper.selectPage(pmsProductAttributePage, lqw);
        //封装对象
        TableDataInfo tInfo = new TableDataInfo();
        tInfo.setList(page1.getRecords());
        tInfo.setPageNum(pageNum);
        tInfo.setPageSize(pageSize);
        tInfo.setTotal(page1.getTotal());
        tInfo.setTotalPage(page1.getPages());
        return AjaxResult.success(tInfo);
    }

    /**
     * 5.修改商品属性信息
     * @param id
     * @param pmsProductAttribute
     * @return
     */
    @Override
    public AjaxResult update(Integer id, PmsProductAttribute pmsProductAttribute) {
        pmsProductAttribute.setId(id);
        pmsProductAttributeMapper.updateById(pmsProductAttribute);
        return AjaxResult.success("修改成功");
    }

    /**
     * 6.查询单个商品属性
     * @param id
     * @return
     */
    @Override
    public AjaxResult findList(Integer id) {
        PmsProductAttribute pmsProductAttribute = pmsProductAttributeMapper.selectById(id);
        return AjaxResult.success(pmsProductAttribute);
    }


}
